import os
import sys
import cv2
import tensorflow as tf
import cocr.sextag as st
import numpy as np
import cocr
from  tensorflow.contrib.layers import *
from tensorflow.contrib.slim.python.slim.nets.inception_v3 import inception_v3_base

class predict:
    def __init__(self):
        self.sexpath='/work/liuwei/model/sex.ckpt'
        self.agepath='/work/liuwei/model/age.ckpt'
        self.expresspath='/work/liuwei/model/express.ckpt'
        self.sextable=['m','f']
        self.agetable=age_table=['0-10','10-20','20-30','30-40','40-50','50-60','60-70','70-80','80-90','90-100']
        self.expresstable=[]



    #sex predict
    def detect(self,image,type):
        modelpath=''
        table=[];
        if(type =='sex'):
            modelpath=self.sexpath
            table=self.sextable
        elif(type=='age'):
            modelpath=self.agepath
            table=self.sextable
        elif(type=='expression'):
            modelpath=self.express
            table=self.expresstable
        labels_size=len(table);
        IMAGE_HEIGHT=112
        IMAGE_WIDTH=92
        X1 = tf.placeholder(dtype=tf.float32, shape=[None, IMAGE_HEIGHT, IMAGE_WIDTH, 3])
        logits = st.conv_net(labels_size, X1)
        saver = tf.train.Saver()
        with tf.Session() as sess:
            saver.restore(sess, self.sexpath )
            softmax_output = tf.nn.softmax(logits)
            batch_results = sess.run(softmax_output, feed_dict={X1: [image]})
            best = np.argmax(batch_results)
            print(best)
            return best;